package org.example.memora.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.memora.DTO.UserRedisDTO;
import org.example.memora.common.result.Result;
import org.example.memora.common.utils.UserHolder;
import org.example.memora.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/follows")
@Tag(name = "用户关注管理", description = "提供关注、取消关注以及查询关注列表的接口")
@Slf4j
public class UserFollowController {

    @Autowired
    private UserFollowService userFollowService;

    @Operation(
            summary = "关注用户",
            description = "用户关注某个博主。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "关注成功", content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "400", description = "关注失败")
    })
    @PostMapping("/follow")
    public Result<?> follow(
            @Parameter(name = "followedId", description = "被关注者ID（博主）", in = ParameterIn.QUERY, required = true)
            @RequestParam Long followedId) {
        UserRedisDTO userRedisDTO = UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID(即followerId),请检查controller");
        }
        Long followerId= userRedisDTO.getUserId();
        log.info("用户[{}]关注用户[{}]", followerId, followedId);
        return userFollowService.follow(followerId, followedId);
    }

    @Operation(
            summary = "取消关注",
            description = "用户取消关注某个博主。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "取消关注成功", content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "400", description = "取消关注失败")
    })
    @DeleteMapping("/unfollow")
    public Result<?> unfollow(

            @Parameter(name = "followedId", description = "被关注者ID（博主）", in = ParameterIn.QUERY, required = true)
            @RequestParam Long followedId) {
        UserRedisDTO userRedisDTO = UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID(即followerId),请检查controller");
        }
        Long followerId= userRedisDTO.getUserId();
        log.info("用户[{}]取消关注用户[{}]", followerId, followedId);
        return userFollowService.unfollow(followerId, followedId);
    }

    @Operation(
            summary = "查询关注列表",
            description = "分页查询指定用户的关注列表。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(mediaType = "application/json"))
    })
    @GetMapping("/list")
    public Result<?> listFollowings(
            @Parameter(name = "page", description = "页码", in = ParameterIn.QUERY, required = true)
            @RequestParam int page,
            @Parameter(name = "pageSize", description = "每页数量", in = ParameterIn.QUERY, required = true)
            @RequestParam int pageSize) {
        UserRedisDTO userRedisDTO = UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID(即followerId),请检查controller");
        }
        Long followerId= userRedisDTO.getUserId();
        log.info("用户[{}]查询关注列表，页码：{}，每页数量：{}", followerId, page, pageSize);
        return userFollowService.listFollowings(followerId, page, pageSize);
    }
}
